package servlet;

import dao.DoctorDao;
import pojo.Department;
import pojo.Doctor;
import pojo.DoctorPage;
import service.DepartmentService;
import service.DepartmentServiceImpl;
import service.DoctorService;
import service.DoctorServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 陈金平
 * @Description
 * @create 2020/11/18 15:06
 */
public class GetAllDoctors extends HttpServlet {
    private DoctorService doctorService = new DoctorServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html");

        Map<String, Object> params = new HashMap<>();

        String page = req.getParameter("page");
        String size = req.getParameter("size");
        String gpub = req.getParameter("pub");

        Integer pid = (page == null) ? 1 : Integer.parseInt(page);
        Integer num = (size == null) ? 2 : Integer.parseInt(size);
        Integer pub = (gpub == null) ? 0 : Integer.parseInt(gpub);

        params.put("page", pid);
        params.put("size", num);
        params.put("pub", pub);

        DoctorPage doctorPage = doctorService.getAllDoctors(params);
        DepartmentService departmentService = new DepartmentServiceImpl();
        List<Department> departments = departmentService.getAllDepartments();

        req.setAttribute("doctorPage", doctorPage);
        req.setAttribute("departments", departments);
        req.setAttribute("pub", pub);

        req.getRequestDispatcher("all_doctors.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
